﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using TouchRC.Classes;
using Fluid.Controls;
using Fluid.Classes;
using TouchRC.Themes;
using System.Runtime.InteropServices;
using TouchRC.Logic;
using TouchRC.MetadataCaching;
using System.IO;

namespace TouchRC
{
    public partial class PasswordForm : Form
    {
        #region PInvoke stuff

        [DllImport("coredll.dll")]
        static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

        [DllImport("coredll.dll", SetLastError=true)]  
        static extern IntPtr SetPowerRequirement(string device, CEDevicePowerState ceDevicePowerState, uint deviceFlags, IntPtr systemState, ulong stateFlags);  
        
        [DllImport("coredll.dll", SetLastError=true)]
        static extern int ReleasePowerRequirement(IntPtr handle);

        [DllImport("coredll.dll", SetLastError=true)]
        static extern void SystemIdleTimerReset();

        public enum CEDevicePowerState
        {
            D0 = 0,    // Full On   
            D1,        // Low On   
            D2,        // Standby   
            D3,        // Sleep   
            D4,        // Off   
        }

        const int SW_MINIMIZED = 6;

        #endregion

        static PasswordForm m_Instance;
        public static PasswordForm Instance { get { return m_Instance; } }

        public new void Hide()
        {
            ShowWindow(this.Handle, SW_MINIMIZED);
        }

        public PasswordForm()
        {
            InitializeComponent();

            m_Instance = this;

            AppSettings.Aquire();

            if (AppSettings.BacklightAlwaysOn)
            {
                // reset the system's idle time every 10 seconds so it doesn't suspend   
                Timer timer = new Timer();
                timer.Interval = 10000;
                timer.Tick += new EventHandler(timer_Tick);
                timer.Enabled = true;
            }
            this.Menu = null;
            this.ControlBox = false;
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            this.Text = "TouchRC";

            Host.Bounds = Screen.PrimaryScreen.Bounds;

            int height = Screen.PrimaryScreen.Bounds.Height;
            int width = Screen.PrimaryScreen.Bounds.Width;

            double ratio = (double)height / (double)width;

            if (ratio == (double)3 / 5 || ratio == (double)5 / 3)
                AppSettings.IsWidescreen = true;

            Rectangle rect = new Rectangle(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y,
                    (int)(Screen.PrimaryScreen.Bounds.Width / Host.ScaleFactor.Width), (int)(Screen.PrimaryScreen.Bounds.Height / Host.ScaleFactor.Width));
            AppSettings.ScreenResolution = rect;

            Theme.CurrentTheme = new DefaultTheme();
            Host.BackColor = Color.Empty;

            //TempPanelScrollTest panel = new TempPanelScrollTest();
            //panel.ShowMaximized();

            //MainMenuPanel panel = new MainMenuPanel(compy);

            //panel.RefreshValues();
            //panel.ShowMaximized();
            //panel.SelectFirstOption();

            ComputerSelectionPanel welcome = new ComputerSelectionPanel();
            welcome.RefreshValues();
            welcome.ShowMaximized();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            // no suspend  
            SystemIdleTimerReset();
        }

        public delegate void DisplayError(string error);

        public void DisplayErrorMethod(string error)
        {
            MessageDialog.Show(error, "Ok", null);
        }
    }
}